﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Convert_DVR_MS_to_iTunes
{
    [System.ComponentModel.DesignerCategory("")]
    //[DefaultProperty("Data2")]
    public class HandbrakeOptions : Component
    {
        // http://msdn.microsoft.com/en-us/library/Aa302334
        // http://msdn.microsoft.com/en-us/library/aa302326.aspx
        // http://www.vbforums.com/showthread.php?t=482212

        public List<string> ParsleyPaths = new List<string>();

        private string m_ConvertorPath = "";
        private string m_ParsleyPath = "";

        private bool m_DeleteSourceFile = false;
        private bool m_DeleteDestinationOnError = true;
        private bool m_AtomicParsley = true;
        private bool m_CopyToiTunes = false;

        public delegate void SearchForParsleyEventHandler(object source, EventArgs e, string value);
        public event SearchForParsleyEventHandler SearchForParsley;
        public virtual void OnSearchForParsley(EventArgs e, string value)
        {
            if (SearchForParsley != null) SearchForParsley(this, e, value);
        }

        [DefaultValue(false)]
        [Category("Conversion")]
        [DisplayName("Delete Source File")]
        [Description("Delete the source file after conversion")]
        public bool DeleteSourceFile
        {
            get { return m_DeleteSourceFile; }
            set { if (value != m_DeleteSourceFile) m_DeleteSourceFile = value; }
        }

        [DefaultValue(true)]
        [Category("Conversion")]
        [DisplayName("Delete Destination On Error")]
        [Description("Delete the destination file on conversion error")]
        public bool DeleteDestinationOnError
        {
            get { return m_DeleteDestinationOnError; }
            set { if (value != m_DeleteDestinationOnError) m_DeleteDestinationOnError = value; }
        }

        [DefaultValue(true)]
        [Category("Meta Tags")]
        [DisplayName("Run AtomicParsley")]
        [Description("Set iTunes metadata in destation file with AtomicParsley")]
        public bool AtomicParsley
        {
            get { return m_AtomicParsley; }
            set { if (value != m_AtomicParsley) m_AtomicParsley = value; }
        }

        [DefaultValue(false)]
        [Category("iTunes")]
        [DisplayName("Copy To iTunes")]
        [Description("Copy destination file to iTunes Auto Import Folder")]
        public bool CopyToiTunes
        {
            get { return m_CopyToiTunes; }
            set { if (value != m_CopyToiTunes) m_CopyToiTunes = value; }
        }

        [DefaultValue("")]
        [ReadOnly(true)]
        [Category("Conversion")]
        [DisplayName("Convertor Full Path")]
        [Description("Location of the convertor file")]
        public string ConvertorPath
        {
            get { return m_ConvertorPath; }
            set { if (value != m_ConvertorPath) m_ConvertorPath = value; }
        }

        [DefaultValue("")]
        [ReadOnly(false)]
        [Category("Meta Tags")]
        [DisplayName("Parsley Full Path")]
        [Description("Location of the parsley application file")]
        [TypeConverter(typeof(StringListConverter))]
        public string ParsleyPath
        {
            get { return m_ParsleyPath; }
            set
            {
                if (value != m_ParsleyPath)
                {
                    if (value.StartsWith("(Search"))
                    {
                        OnSearchForParsley(new EventArgs(), value);
                    }
                    else
                        m_ParsleyPath = value;
                }
            }
        }

        [Browsable(false)]
        public string SettingsString
        {
            get
            {
                return "DeleteSourceFile=" + m_DeleteSourceFile.ToString() + "," +
                    "DeleteDestinationOnError=" + m_DeleteDestinationOnError.ToString() + "," +
                    "AtomicParsley=" + m_AtomicParsley.ToString() + "," +
                    "CopyToiTunes=" + m_CopyToiTunes.ToString() + "," +
                    "ConvertorPath=" + m_ConvertorPath.ToString() + "," +
                    "ParsleyPath=" + m_ParsleyPath.ToString();
            }
            set
            {
                //TODO:DS Extract saved options
                if (value != String.Empty)
                {
                    string[] options = value.Split(',');

                    foreach (string s in options)
                    {
                        if (s != String.Empty)
                        {
                            if (s.Substring(0, s.IndexOf('=')) == "DeleteSourceFile") m_DeleteSourceFile = Convert.ToBoolean(s.Substring(s.IndexOf('=') + 1));
                            if (s.Substring(0, s.IndexOf('=')) == "DeleteDestinationOnError") m_DeleteDestinationOnError = Convert.ToBoolean(s.Substring(s.IndexOf('=') + 1));
                            if (s.Substring(0, s.IndexOf('=')) == "AtomicParsley") m_AtomicParsley = Convert.ToBoolean(s.Substring(s.IndexOf('=') + 1));
                            //"CopyToiTunes=" + m_CopyToiTunes.ToString() +
                            //"ConvertorPath=" + m_ConvertorPath.ToString() +
                            //"ParsleyPath=" + m_ParsleyPath.ToString();
                        }
                    }
                }
            }
        }
    }

    [System.ComponentModel.DesignerCategory("")]
    public class StringListConverter : TypeConverter
    {
        public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
        {
            return true; // display drop
        }

        public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
        {
            return true; // drop-down vs combo
        }

        public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
        {
            return new StandardValuesCollection(((HandbrakeOptions)context.Instance).ParsleyPaths.ToArray());
        }
    }
}
